package cn.legend.travel.admin.content.dao.persist.repository.impl;

import cn.legend.travel.admin.content.dao.persist.mapper.GuideMapper;
import cn.legend.travel.admin.content.dao.persist.repository.IGuideRepository;
import cn.legend.travel.admin.content.pojo.entity.Guide;
import cn.legend.travel.admin.content.pojo.param.GuideIssueParam;
import cn.legend.travel.admin.content.pojo.vo.GuideCheckVO;
import cn.legend.travel.admin.content.pojo.vo.GuideDetailVO;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class GuideRepositoryImpl implements IGuideRepository {
    private final GuideMapper mapper;

    @Override
    public int check(Long id, Integer state) {
        Guide guide = new Guide();
        guide.setId(id);
        guide.setState(state);
        return mapper.updateById(guide);
    }

    @Override
    public int changeDisplay(Long id, Integer isDisplay) {
        Guide guide = new Guide();
        guide.setId(id);
        guide.setIsDisplay(isDisplay);
        return mapper.updateById(guide);
    }

    @Override
    public PageData<GuideCheckVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.list()));
    }

    @Override
    public GuideDetailVO getDetail(Long id) {
        return mapper.getDetail(id);
    }

    @Override
    public Long insertBasic(GuideIssueParam param) {
        Guide guide = new Guide(param);
        int row = mapper.create(guide);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试");
        }
        return guide.getId();
    }

}
